﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="ShopPowerTemplate.SearchResultsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="using:ShopPowerTemplate.Common"
    xmlns:converters="using:ShopPowerTemplate.Common"
    xmlns:controls="using:ShopPowerTemplate.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:ExtensionType="Search">

    <Page.Resources>
        <CollectionViewSource x:Name="resultsViewSource" Source="{Binding Results}"/>
        <CollectionViewSource x:Name="filtersViewSource" Source="{Binding Filters}"/>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyleCustom}" />
            <StackPanel x:Name="headerStackPanel" Grid.Column="1" Orientation="Horizontal">
                <Border x:Name="headerBorder" Height="80" Padding="0,0,20,0" Margin="0,0,20,0" BorderThickness="0,0,1,0" BorderBrush="{StaticResource ApplicationHeaderForegroundThemeBrush}">
                    <Image x:Name="headerImage" Source="ms-appx:///Assets/LogoShop.png" Stretch="Uniform" />
                </Border>
                <TextBlock x:Name="pageTitle" Text="{Binding Title}" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
            </StackPanel>
            <controls:ShoppingCartButton x:Name="shoppingCartButton" Grid.Column="1" Margin="0,0,50,0" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        </Grid>

        <!-- Subtitles-->
        <Grid x:Name="queryGrid" Grid.Row="1" Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="resultText" Text="Resultados" Style="{StaticResource PageSubheaderTextStyle}" />
            <TextBlock x:Name="queryText" Grid.Column="1" Text="{Binding QueryText}" Style="{StaticResource PageSubheaderTextStyle}" />
        </Grid>

        <!-- Sorting combo -->
        <ComboBox x:Name="sortCombo" 
                  ItemsSource="{Binding SortItems}" 
                  SelectedItem="{Binding SelectedSort, Mode=TwoWay}" 
                  DisplayMemberPath="Name" 
                  Grid.Column="1"
                  Grid.Row="2" 
                  Margin="0,0,0,20" 
                  MinWidth="180" 
                  HorizontalAlignment="Left" />

        <!--
            The body of the page in most view states uses an items controls to create multiple radio buttons
            for filtering above a horizontal scrolling grid of search results
        -->
        <Grid x:Name="typicalPanel" Grid.Row="3" Grid.Column="1">

            <ScrollViewer
                x:Name="resultsGridScrollViewer"
                Grid.Row="1"
                AutomationProperties.AutomationId="ResultsGridScrollViewer"
                Margin="0,-4,0,0"
                Style="{StaticResource HorizontalScrollViewerStyle}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ItemsControl
                        x:Name="filtersItemsControl"
                        ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                        Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                        Margin="0,-3,40,30">

                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <RadioButton
                            Content="{Binding Description}"
                            GroupName="Filters"
                            IsChecked="{Binding Active, Mode=TwoWay}"
                            Checked="Filter_Checked"
                            Margin="0,0,40,0"
                            Style="{StaticResource TextRadioButtonStyle}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <GridView
                        x:Name="resultsGridView"
                        SelectionMode="None"
                        ItemClick="OnItemClick"
                        IsItemClickEnabled="True"
                        AutomationProperties.AutomationId="ResultsGridView"
                        AutomationProperties.Name="Search Results"
                        ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                        ItemTemplate="{StaticResource SearchResultItemTemplate}"
                        Margin="0,0,40,46"
                        Grid.Row="1"/>
                </Grid>
            </ScrollViewer>
        </Grid>

        <!--
            The body of the page when snapped uses a combo box to select a filter above a
            vertical scrolling list of search results
        -->
        <Grid x:Name="snappedPanel" Grid.Row="3" Visibility="Collapsed" Grid.ColumnSpan="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <ComboBox
                ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                Margin="20,0,20,20"
                SelectionChanged="Filter_SelectionChanged">
            </ComboBox>

            <ListView
                x:Name="resultsListView"
                Grid.Row="1" 
                Margin="16,0,0,0"
                SelectionMode="None"
                ItemClick="OnItemClick"
                IsItemClickEnabled="True"
                ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                ItemTemplate="{StaticResource SearchResultItemTemplate}"/>
        </Grid>
        <Border x:Name="bottomShoppingCartButton" Height="60" Grid.Row="4" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{StaticResource ApplicationHeaderForegroundThemeBrush}" Visibility="Collapsed">
            <controls:ShoppingCartButton x:Name="shoppingCartButton1" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource ApplicationPageBackgroundThemeBrush}"/>
        </Border>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup>
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="filtersItemsControl" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,-3,90,30"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsGridView" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="96,0,86,56"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped in addition to more extensive changes:
                    * The query text becomes the page header
                    * A different representation for filter selection and search results is used
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedButtonStyleCustom}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="shoppingCartButton">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="bottomShoppingCartButton">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Column)" Storyboard.TargetName="headerStackPanel">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.ColumnSpan)" Storyboard.TargetName="headerStackPanel">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)" Storyboard.TargetName="headerStackPanel">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Center"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(StackPanel.Orientation)" Storyboard.TargetName="headerStackPanel">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Vertical"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="headerBorder">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)" Storyboard.TargetName="headerBorder">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Center"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="headerBorder">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)" Storyboard.TargetName="pageTitle">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Center"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="pageTitle">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0,10"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Style)" Storyboard.TargetName="pageTitle">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedTitleTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="headerImage">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="40,20,0,0"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultText" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="queryText" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="typicalPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="snappedPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="sortCombo" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="20,0,20,20"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="sortCombo" Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="sortCombo" Storyboard.TargetProperty="(Grid.ColumnSpan)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="queryGrid" Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="queryGrid" Storyboard.TargetProperty="(Grid.ColumnSpan)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="queryGrid" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="20,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>